import base64


def detect_encoding(data):
    try:
        # 尝试使用 Base64 解码数据
        decoded_data = base64.b64decode(data)
        return "Base64"
    except:
        # 解码失败，可能是二进制数据
        return "Binary"


# base64编码到二进制流
def base64_to_binary(image_base64):
    if isinstance(image_base64, bytes):
        image = image_base64
        return image

    if ',' in image_base64:
        image_base64 = image_base64.split(',')[1]

    image = base64.b64decode(image_base64)
    return image


# 二进制流到base64
def binary_to_base64(image):
    if not isinstance(image, bytes):
        image_base64 = image
        return image_base64

    image = base64.b64encode(image).decode("utf-8")
    image_base64 = "data:image/jpeg;base64," + image
    return image_base64
